﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AircraftBattleDB
{
    public class BattleDB
    {
        private static int craftIDMax = 0;
        private static int userIDMax = 0;

        public int GetAircarftID(string username)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<user> q = from u in context.users
                                 where u.name == username
                                 select u;

            if (!q.Any<user>()) throw new Exception("User '" + username + "' does not exist.");

            int aid = q.First<user>().craftid;

            IQueryable<aircraft> p = from a in context.aircraft
                                     where a.id == aid
                                     select a;

            if (!p.Any<aircraft>()) throw new Exception("User '"
                + username + "' does not have any aircraft in db.");

            return p.First<aircraft>().id;

        }

        public bool IfUserExist(string username)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<user> q = from u in context.users
                                 where u.name == username
                                 select u;

            return q.Any<user>();
        }

        public bool IsAdmin(string username)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<user> q = from u in context.users
                                 where u.name == username
                                 select u;

            if (!q.Any<user>()) throw new Exception("User '" + username + "' does not exist.");

            return q.First<user>().isadmin;
        }

        public void CreateUser(string username)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            if (username == "")
                throw new Exception("Username cannot be empty.");
            if (username.Length > 10)
                throw new Exception("Username must be less than 11 characters");

            if (craftIDMax == 0)
            {
                IQueryable<int> q = from craft in context.aircraft
                                    select craft.id;
                craftIDMax = q.Max();
                craftIDMax++;
            }

            aircraft a = aircraft.Createaircraft(craftIDMax, 1, 5, 0, 0);
            craftIDMax++;

            if (userIDMax == 0)
            {
                IQueryable<int> q = from ur in context.users
                                    select ur.id;
                userIDMax = q.Max();
                userIDMax++;
            }

            user u = user.Createuser(userIDMax, username, "12345", false, a.id, 0, 0, 0, 0, false);
            userIDMax++;

            context.aircraft.AddObject(a);
            context.users.AddObject(u);
            context.SaveChanges();
        }

        public string GetPassword(string username)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<user> p = from u in context.users
                                 where u.name == username
                                 select u;

            return p.First<user>().password;
        }

        public void AddKillRecord(string username)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<user> r = from u in context.users
                                 where u.name == username
                                 select u;

            if (!r.Any<user>()) throw new Exception("User '" + username + "' does not exist.");
            
            r.First<user>().kills++;

        }

        public void AddWinRecord(string username)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<user> r = from u in context.users
                                 where u.name == username
                                 select u;

           if (!r.Any<user>()) throw new Exception("User '" + username + "' does not exist.");
            
            r.First<user>().win++;
            context.SaveChanges();
        }

        public void AddLoseRecord(string username)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<user> r = from u in context.users
                                 where u.name == username
                                 select u;

             if (!r.Any<user>()) throw new Exception("User '" + username + "' does not exist.");
            
            r.First<user>().lose++;
            context.SaveChanges();
        }

        public int GetWinRecord(string username)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<user> r = from u in context.users
                                 where u.name == username
                                 select u;

            if (!r.Any<user>()) throw new Exception("User '" + username + "' does not exist.");

            return r.First<user>().win;
        }

        public int GetLoseRecord(string username)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<user> r = from u in context.users
                                 where u.name == username
                                 select u;

            if (!r.Any<user>()) throw new Exception("User '" + username + "' does not exist.");

            return r.First<user>().lose;
        }

        public bool GetBlockInfo(string username)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<user> r = from u in context.users
                                 where u.name == username
                                 select u;

            if (!r.Any<user>()) throw new Exception("User '" + username + "' does not exist.");

            return r.First<user>().blocked;
        }

        public void SetBlockInfo(string username, bool blocked)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<user> r = from u in context.users
                                 where u.name == username
                                 select u;

            if (!r.Any<user>()) throw new Exception("User '" + username + "' does not exist.");

            r.First<user>().blocked = blocked;
            context.SaveChanges();
        }

        public int GetMaxUserID()
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<int> q = from u in context.users
                                 select u.id;
            return q.Max();
        }

        public string GetUserName(int id)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<user> q = from u in context.users
                                 where u.id == id
                                 select u;

            if (!q.Any<user>()) return null;

            return q.First<user>().name;
        }
                                 

        public bool UpgrageW(int craftId)
        {
            AircraftBattleEntities context = new AircraftBattleEntities();

            IQueryable<user> i = from u in context.users
                                 where u.craftid == craftId
                                 select u;

            if (!i.Any<user>()) throw new Exception("Aircraft " + "No. " + craftId + " not exist!");

            int id = i.First<user>().id;

            IQueryable<aircraft> w = from u in context.aircraft
                                     where u.id == id
                                     select u;

            if (w.Any<aircraft>())
            {
                int level = w.First<aircraft>().weapon_id;
                if (level < 3)
                {
                    level++;
                    return true;
                }
                else
                    return false;
            }
            else
                throw new Exception("No record about user No. " + id + "'s aircraft.");

        }
    }
}
